package com.jxd.ordersys.controller;

import com.jxd.ordersys.model.Dept;
import com.jxd.ordersys.model.Emp;
import com.jxd.ordersys.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DeptController
 * @Description TODO
 * @Author 王云超
 * @Date 2024/9/20 10:24
 * @Version 1.0
 */
@RestController
public class DeptController {
    @Autowired
    IDeptService deptService;

    @RequestMapping("/getDeptByPage")
    public Map<String,Object> getDeptByPage(@RequestBody Map<String,Object> queryMap){
        Integer page = (Integer) queryMap.get("page");
        Integer limit = (Integer) queryMap.get("limit");
        String deptName = (String) queryMap.get("deptName");
        Map<String,Object> response = new HashMap<>();
        List<Dept> depts = deptService.getDeptByPage(page,limit,deptName);
        int count  = deptService.getDeptCount(deptName);
        response.put("depts",depts);
        response.put("count",count);
        return response;
    }

    @RequestMapping("/getDeptById/{deptId}")
    public Dept getDeptById(@PathVariable("deptId") Integer deptId){
        return deptService.getDeptById(deptId);
    }

    @RequestMapping("/deptList")
    public List<Dept> getDeptList(){
        return deptService.deptList();
    }

    @RequestMapping("addDept")
    public String addDept(@RequestBody Dept dept){
        boolean addFlag = deptService.addDept(dept);
        if (addFlag) {
            return "success";
        } else {
            return "error";
        }
    }

    @RequestMapping("/editDept")
    public String editEmp(@RequestBody Dept dept) {
        boolean editFlag = deptService.editDept(dept) ;
        if (editFlag) {
            return "success";
        } else {
            return "error";
        }
    }

    @RequestMapping("/delDept")
    public String delDept(@RequestBody List<Integer> deptIds) {
        boolean delFlag = deptService.delDept(deptIds) ;
        if (delFlag) {
            return "success";
        } else {
            return "error";
        }
    }

}